#include "showindexdialog.h"
#include "ui_showindexdialog.h"
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>

ShowIndexDialog::ShowIndexDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ShowIndexDialog)
{
    ui->setupUi(this);

    this->setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint);
}

ShowIndexDialog::~ShowIndexDialog()
{
    delete ui;
}

void ShowIndexDialog::setFileList(QStringList list) {
    this->list = list;
}

void ShowIndexDialog::showEvent(QShowEvent *) {
    this->ui->index_file_list->clear();
    for (QString str : this->list) {
        this->ui->index_file_list->addItem(str);
    }
}

void ShowIndexDialog::on_index_file_list_clicked(const QModelIndex &index)
{
    QString index_str = index.data().toString().replace(".db", "");
    QFile conf_file("./index/" + index_str + ".txt");
    if (!conf_file.open(QFile::ReadOnly | QFile::Text)) {
        QMessageBox::critical(this, "系统错误", "无法读取文件" + index_str);
        return;
    }
    QTextStream ts(&conf_file);
    QString content = ts.readLine();
    conf_file.close();
    emit this->send_db_name(content);
}
